﻿using B.S.DataBase.MyProject.Domain;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure.Interface;
using B.S.DataBase.MyRead.API.Appicant.Command.ApproveCommand;
using MediatR;

namespace B.S.DataBase.MyRead.API.Appicant.CommandHandler.ApproveCommandHandler
{
    /// <summary>
    /// 获取上一级领导名称(多人) (部门id,级别) 命令执行
    /// </summary>
    public class FanTianleaveLeaderNameCommandHandler : IRequestHandler<FanTianleaveLeaderNameCommand, APIResult<List<User>>>
    {
        private readonly IBaseRepository<User> userRepos;

        public FanTianleaveLeaderNameCommandHandler(IBaseRepository<User> userRepos)
        {
            this.userRepos = userRepos;
        }
        /// <summary>
        ///  获取上一级领导名称(多人) (部门id,级别) 命令执行
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<APIResult<List<User>>> Handle(FanTianleaveLeaderNameCommand request, CancellationToken cancellationToken)
        {
            APIResult<List<User>> result = new APIResult<List<User>>();
            var list = userRepos.GetAll().Result.Where(x=>x.DepId == request.DepId && x.LevelRoleId == request.LevelRoleId);
            //失败情况
            if (list == null)
            {
                result.Code = APIEnum.查询失败;
                result.Msg = APIEnum.查询失败.ToString();
                result.data = list.ToList();
            }else
            {
                int num = list.Count();
                result.Code = APIEnum.查询成功;
                result.Msg = APIEnum.查询成功.ToString() + num + "条";
                result.data = list.ToList();
            }
            return await Task.FromResult(result);
        }
    }
}
